package com.example.test2023.Service.impl;

import com.example.test2023.Dao.UserDao;
import com.example.test2023.Service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestServiceImpl implements TestService {

    private UserDao userDao;


         //1.基于xml的依赖注入

//    set方法
/*    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    } */

//构造方法
 /*  public TestServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }
     //使用有参构造后面要添加无参构造
    public TestServiceImpl() {
    }*/




    //2.基于注解的依赖注入

    @Autowired
//    @Qualifier("userDao01")
    //@Autowired是根据类型进行自动装配的。如果当spring上下文中存在不止一个UserDao类型的bean时，就会抛出BeanCreationException异常;如果Spring上下文中不存在UserDao类型的bean，也会抛出BeanCreationException异常。我们可以使用@Qualifier配合@Autowired来解决这些问题
    public UserDao getUserDao() {
        return userDao;
    }
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void save() {
        userDao.userDao();
    }



    //3.基于Java的依赖注入
   /* @Override
    public void save() {
        ClassPathXmlApplicationContext app=
                new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao= (UserDao) app.getBean("userDao");
        userDao.userDao();
    }*/
}
